package DBClass;

use base 'DBIX::DBClass';

#sub _checktable
#{
#	my $self = shift;
#    my $tablename = shift;
#    my $regex     = qr/$tablename/;
#    my $sth       = $self->{'dbh'}->table_info( '', 'public', undef, 'TABLE' );
#    for my $rel ( @{ $sth->fetchall_arrayref( {} ) } ) {
#        return $rel->{TABLE_NAME} if $rel->{TABLE_NAME} =~ m/$regex/;
#    }
#}



sub get_all
{
	my $self = shift;
	my $table = shift;
	my $query = shift || '';
	
	my $where = ($query =~ m/\S/)
				? "where $query"
				: "";
	my $sql_cmd = "select *"
				. " from $table $where";
	my $sth = $self->{'dbh'}->prepare($SQL);
	$sth->execute();
	return $sth->fetchrow_arrayref;
}

sub _createtable
{
	my $self = shift;
    my $tablename = shift;
    my $SQL	=  qq{
                create table $tablename (
                id serial primary key,
                req_time NUMERIC(7,3),
                upstream_resp NUMERIC(7,3),
                upstream_host char(15),
                time time without time zone not null,
                ip char(15) not null,
                hostname varchar(255),
                url varchar(255) not null,
                code int,
                bytes_sent int,
                referer varchar(255),
                useragent varchar(255)
        	)
    	};;
    my $sth = $self->{'dbh'}->prepare($SQL);
    $sth->execute() or die $self->{'dbh'}->errstr;
}

sub _droptable
{
	my $self = shift;
    my $tablename = shift;
    my $SQL       = qq{
        drop table $tablename
    };
    my $sth = $self->{'dbh'}->do($SQL) or die $self->{'dbh'}->errstr;
}

1;
